import Vue from 'vue'
import VueRouter from 'vue-router'
import { authentication } from '@/api/auth'
// import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/email',
    name: 'Email',
    component: () => import('@/view/Email/Email.vue')
  },
  {
    path: '/admin',
    name: 'AdminManagement',
    meta: {
      requireAuth: true
    },
    component: () => import('@/view/AdminManagement/AdminIndex.vue'),
    redirect: '/admin/resousemanagement',
    beforeEnter: (to, from, next) => {
      const { userType } = JSON.parse(window.localStorage.getItem('user'))
      if (to.meta.requireAuth) {
        if (userType === 2) {
          next()
        } else {
          next(false)
          alert('您没有权限进入管理员页面')
        }
      }
    },
    children: [
      {
        path: 'resousemanagement',
        name: 'ResouseManagement',
        meta: {
          requireAuth: true
        },
        component: () => import('@/view/AdminManagement/ResouseManagement.vue')
      },
      {
        path: 'upload',
        name: 'ResourceUpload',
        meta: {
          requireAuth: true
        },
        component: () => import('@/view/AdminManagement/ResourceUpload.vue')
      },
      {
        path: 'advicemanagement',
        name: 'AdviceManagement',
        meta: {
          requireAuth: true
        },
        component: () => import('@/view/AdminManagement/AdviceManagement.vue')
      }, {
        path: 'teachingsclicemanagement',
        name: 'ScliceOfTeachingPlaneManagement',
        meta: {
          requireAuth: true
        },
        component: () => import('@/view/AdminManagement/SliceOfTeachingPlaneManagement.vue')
      }
    ]
  },
  {
    path: '/new',
    name: 'new',
    component: () => import('@/view/new')
  },
  {
    path: '/catalog',
    name: 'catalog',
    component: () => import('@/components/NavMenu/CatalogueSelect')
  },
  {
    path: '/homepage',
    name: 'HomePage',
    component: () => import('@/view/HomePage')
  },
  {
    path: '/',
    name: 'newHomePage',
    component: () => import('@/view/newHomePage')
  },
  {
    path: '/search',
    name: 'ResourceCenter',
    component: () => import('@/view/ResourceCenter')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/view/Sign')
  },
  // 忘记密码
  {
    path: '/retrievePassword',
    name: 'ResetPwd',
    component: () => import('@/view/ResetPwd')
  },
  {
    path: '/account',
    name: 'Account',
    component: () => import('@/view/Account/Account'),
    meta: {
      requireAuth: true
    },
    redirect: '/account/home',
    children: [
      {
        path: 'home',
        name: 'UserInfo',
        component: () => import('@/view/Account/Home'),
        meta: {
          requireAuth: true,
          title: '个人中心'
        }
      },
      {
        path: 'introduce',
        name: 'IntroduceInfo',
        component: () => import('@/view/Introduce/Introduce.vue'),
        meta: {
          requireAuth: true,
          title: '了解我们'
        }
      }
    ]
  },
  // 收藏
  {
    path: '/package',
    name: 'Package',
    component: () => import('@/view/Package'),
    meta: {
      requireAuth: true,
      title: '资源包'
    }
  },
  // 资源页面
  {
    path: '/resource/:resourceID',
    name: 'Resource',
    component: () => import('@/view/Resource')
  },
  // 排行页面
  {
    path: '/rate',
    name: 'Rate',
    component: () => import('@/view/Rate')
  },
  // 知识页面
  {
    path: '/knowledge/:entityName',
    name: 'Knowledge',
    component: () => import('@/view/Knowledge')
  },
  {
    path: '/instruction',
    name: 'Instruction',
    component: () => import('@/view/Instruction'),
    redirect: '/instruction/teachingPlan',
    children: [
      {
        path: 'teachingPlan',
        name: 'TeachingPlane',
        component: () => import('@/view/Instruction/TeachingPlan.vue')
      },
      {
        path: 'examination',
        name: 'Examination',
        component: () => import('@/view/Instruction/Examination.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  // base: '/e-resource/',
  routes,
  // 跳转路由时的滚动问题
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else if (to.hash) {
      const positon = {}
      if (to.hash) {
        positon.selector = to.hash
        return false
      }
    } else {
      return { x: 0, y: 0 }
    }
  }
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    // 判断是否有标题
    document.title = to.meta.title
  }

  // 如果访问的页面需要登录
  if (to.meta.requireAuth) {
    authentication().then(response => {
      const {
        data: { code }
      } = response
      if (code !== 200) {
        // 未登录
        next({
          path: 'login',
          query: {
            redirect: to.fullPath !== '/admin/resousemanagement' ? to.fullPath : '/'
          }
        })
      } else {
        next()
      }
    })
  } else {
    next()
  }
})

export default router
